<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <div class="layui-form-block">
          <button type="button" class="layui-btn layui-btn-primary" onclick="checkAll('#LAY-auth-tree-index')">全选
          </button>
          <button type="button" class="layui-btn layui-btn-primary" onclick="uncheckAll('#LAY-auth-tree-index')">全不选
          </button>
          <button type="button" class="layui-btn layui-btn-primary" onclick="showAll('#LAY-auth-tree-index')">全部展开
          </button>
          <button type="button" class="layui-btn layui-btn-primary" onclick="closeAll('#LAY-auth-tree-index')">全部隐藏
          </button>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选择权限</label>
        <div class="layui-input-block">
            <div id="LAY-auth-tree-index"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" type="submit" lay-submit lay-filter="LAY-auth-tree-submit">提交</button>
            <button class="layui-btn layui-btn-primary" type="reset">重置</button>
        </div>
    </div>
</div>
</div>
<script src="/static/admin/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/admin/layuimini/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script>
    // 全选样例
    function checkAll(dst){
        layui.use(['jquery', 'layer', 'authtree'], function(){
            var layer = layui.layer;
            var authtree = layui.authtree;

            authtree.checkAll(dst);
        });
    }
    // 全不选样例
    function uncheckAll(dst){
        layui.use(['jquery', 'layer', 'authtree'], function(){
            var layer = layui.layer;
            var authtree = layui.authtree;

            authtree.uncheckAll(dst);
        });
    }
    // 显示全部
    function showAll(dst){
        layui.use(['jquery', 'layer', 'authtree'], function(){
            var layer = layui.layer;
            var authtree = layui.authtree;

            authtree.showAll(dst);
        });
    }
    // 隐藏全部
    function closeAll(dst){
        layui.use(['jquery', 'layer', 'authtree'], function(){
            var layer = layui.layer;
            var authtree = layui.authtree;

            authtree.closeAll(dst);
        });
    }
    layui.config({
        base: '/static/admin/layuimini/js/lay-module/authtree/layui_exts/',
    }).extend({
        authtree: 'authtree',
    });
    layui.use(['jquery', 'authtree', 'form', 'layer'], function(){
        var $ = layui.jquery;
        var authtree = layui.authtree;
        var form = layui.form;
        var layer = layui.layer;
        layer.msg("加载中",{
            icon:16,
            time:-1
        })
        // 一般来说，权限数据是异步传递过来的
        $.ajax({
            url: "{:url('role/authorize')}"+"?_action=getAuthorizeList&id="+"{$roleId}",
            dataType: 'json',
            success: function(data){
                var trees = data.data.trees;
                // 如果后台返回的不是树结构，请使用 authtree.listConvert 转换
                authtree.render('#LAY-auth-tree-index', trees, {
                    inputname: 'authids[]', 
                    layfilter: 'lay-check-auth', 
                    autowidth: true,
                });
                layer.closeAll();
            }
        });

        // 权限更新提交操作
        form.on('submit(LAY-auth-tree-submit)', function(obj){
            var authids = authtree.getChecked('#LAY-auth-tree-index');
            console.log('Choosed authids is', authids);
            obj.field.authids = authids;
            $.ajax({
                url: "{:url('role/authorize')}"+"?_action=authorize&id="+"{$roleId}",
                type: 'post',
                dataType: 'json',
                data: obj.field,
                success: function(res){
                   layer.msg(res.msg);
                   if(res['status']){
                        setTimeout(function(){
                            if(window !== parent.window) parent.window.layer.closeAll();
                        }, 1000)
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>